package com.bana.fvs.app.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bana.fvs.R;


/**
 * Created by wei on 2017/10/16.
 */
public class TextImageLayout extends FrameLayout {

    private Context mContext;
    private TextView tv_left, tv_right;
    private ImageView iv_left, iv_right;
    private TextView tv_line;
    private String rightText;

    public TextImageLayout(Context context) {
        this(context, null);
    }

    public TextImageLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TextImageLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        View.inflate(context, R.layout.layout_textimagelayout, this);
        iv_left = findViewById(R.id.tl_iv_left);
        tv_left = findViewById(R.id.tl_tv_left);
        tv_right = findViewById(R.id.tl_tv_right);
        iv_right = findViewById(R.id.tl_iv_right);
        tv_line = findViewById(R.id.tl_tv_line);
        getAttribute(attrs);
    }

    private void getAttribute(AttributeSet attrs) {
        TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.TextImageLayout);

        String leftText = "", rightText = "";
        if (ta.hasValue(R.styleable.TextImageLayout_leftText)) {
            leftText = ta.getString(R.styleable.TextImageLayout_leftText);
            if (TextUtils.isEmpty(leftText)) {
                int textRes = ta.getResourceId(R.styleable.TextImageLayout_leftText, -1);
                if (textRes != -1) {
                    leftText = getResources().getString(textRes);
                }
            }
        }
        if (ta.hasValue(R.styleable.TextImageLayout_rightText)) {
            rightText = ta.getString(R.styleable.TextImageLayout_rightText);
            if (TextUtils.isEmpty(leftText)) {
                int textRes = ta.getResourceId(R.styleable.TextImageLayout_rightText, -1);
                if (textRes != -1) {
                    rightText = getResources().getString(textRes);
                }
            }
        }


        int defaultColor = -10;
        int leftTextColor = defaultColor, rightTextColor = defaultColor;
        if (ta.hasValue(R.styleable.TextImageLayout_leftTextColor)) {
            leftTextColor = ta.getColor(R.styleable.TextImageLayout_leftTextColor, defaultColor);
            if (leftTextColor == -1) {
                leftTextColor = ta.getResourceId(R.styleable.TextImageLayout_leftTextColor, defaultColor);
            }
        }
        if (ta.hasValue(R.styleable.TextImageLayout_rightTextColor)) {
            rightTextColor = ta.getColor(R.styleable.TextImageLayout_rightTextColor, defaultColor);
            if (rightTextColor == defaultColor) {
                rightTextColor = ta.getResourceId(R.styleable.TextImageLayout_rightTextColor, defaultColor);
            }
        }
        float leftTextSize = -1, rightTextSize = -1;
        if (ta.hasValue(R.styleable.TextImageLayout_leftTextSize)) {
            leftTextSize = ta.getDimension(R.styleable.TextImageLayout_leftTextSize, dp2px(16));
        }
        if (ta.hasValue(R.styleable.TextImageLayout_rightTextSize)) {
            rightTextSize = ta.getDimension(R.styleable.TextImageLayout_rightTextSize, dp2px(16));
        }

        //左侧图片
        boolean leftVisible = false;
        if (ta.hasValue(R.styleable.TextImageLayout_leftImageVisiblity)) {
            leftVisible = ta.getBoolean(R.styleable.TextImageLayout_leftImageVisiblity, false);
        }
        iv_left.setVisibility(leftVisible ? View.VISIBLE : View.GONE);
        if (leftVisible) {
            if (ta.hasValue(R.styleable.TextImageLayout_leftImage)) {
                Drawable drawable = ta.getDrawable(R.styleable.TextImageLayout_leftImage);
                iv_left.setImageDrawable(drawable);
            }
        }

        //右侧图片
        Drawable drawable = null;
        if (ta.hasValue(R.styleable.TextImageLayout_rightImage)) {
            drawable = ta.getDrawable(R.styleable.TextImageLayout_rightImage);
        }
        boolean isShow = false;
        if (ta.hasValue(R.styleable.TextImageLayout_rightImageVisiblity)) {
            isShow = ta.getBoolean(R.styleable.TextImageLayout_rightImageVisiblity, false);
        }
        int rightTextBg = -1;
        Drawable rightTextDrawable = null;
        if (ta.hasValue(R.styleable.TextImageLayout_rightTextBackground)) {
            rightTextBg = ta.getResourceId(R.styleable.TextImageLayout_rightTextBackground, -1);
            if (rightTextBg == -1) {
                rightTextDrawable = ta.getDrawable(R.styleable.TextImageLayout_rightTextBackground);
            }
        }


        //底部间隔线
        boolean isShowLine = true;
        if (ta.hasValue(R.styleable.TextImageLayout_lineVisiblity)) {
            isShowLine = ta.getBoolean(R.styleable.TextImageLayout_lineVisiblity, true);
        }
        tv_line.setVisibility(isShowLine ? View.VISIBLE : View.GONE);

        ta.recycle();

        //左侧文字
        if (!TextUtils.isEmpty(leftText)) {
            tv_left.setText(leftText);
        }

        if (leftTextColor != defaultColor) {
            tv_left.setTextColor(leftTextColor);
        }
        if (leftTextSize != -1) {
            tv_left.setTextSize(TypedValue.COMPLEX_UNIT_DIP, px2dp(leftTextSize));
        }

        //右侧文字
        if (!TextUtils.isEmpty(rightText)) {
            setRightText(rightText);
        }
        if (rightTextColor != defaultColor) {
            tv_right.setTextColor(rightTextColor);
        }
        if (rightTextSize != -1) {
            tv_right.setTextSize(TypedValue.COMPLEX_UNIT_DIP, px2dp(rightTextSize));
        }
        if (rightTextBg != -1) {
            tv_right.setBackgroundResource(rightTextBg);
        } else if (rightTextDrawable != null) {
            tv_right.setBackground(rightTextDrawable);
        }

        //箭头图片
        if (drawable != null) {
            iv_right.setImageDrawable(drawable);
        }
        iv_right.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    public void setLeftText(String text) {
        tv_left.setText(text);
    }

    public void setRightText(String text) {
        rightText = text;
        tv_right.setText(text);
    }

    /**
     * 设置右侧图片是否显示
     *
     * @param visiable
     */
    public void setRightImageVisible(int visiable) {
        iv_right.setVisibility(visiable);
    }


    private float dp2px(float dpValue) {
        float density = mContext.getResources().getDisplayMetrics().density;
        return dpValue * density + 0.5f;
    }

    private float px2dp(float pxValue) {
        float density = mContext.getResources().getDisplayMetrics().density;
        return pxValue / density - 0.5f;
    }

    public String getRightText() {
        return rightText;
    }
}
